﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using NIEN_LUAN.Db;
using System.Windows.Forms;

namespace NIEN_LUAN
{
    public partial class FormLapPhieuThu : Form
    {
        private DataContentDataContext db = new DataContentDataContext();
        public FormThongKePhieuThu frmXemPhieuThu;
        public FormTimKiemPhieuThu frmXemphieuthu;
        public Db.PHIEUTHU PhieuThuCanin;

        public FormLapPhieuThu()
        {
            InitializeComponent();
            this.btnHuyPT.Enabled = false;
            this.btnInPT.Enabled = false;
            this.btnLuuPT.Enabled = false;
            this.btnThoat.Enabled = true;
            this.btnVietPTmoi.Enabled = true;
            this.groupBox1.Enabled = false;
        }

        public FormLapPhieuThu(FormThongKePhieuThu frm,Db.PHIEUTHU phieuthucanin)
        {
            InitializeComponent();
            this.btnHuyPT.Enabled = false;
            this.btnInPT.Enabled = true;
            this.btnLuuPT.Enabled = false;
            this.btnThoat.Enabled = true;
            this.btnVietPTmoi.Enabled = true;
            this.groupBox1.Enabled = false;
            this.frmXemPhieuThu=frm;
            this.PhieuThuCanin=phieuthucanin;
        }

        public FormLapPhieuThu(FormTimKiemPhieuThu frm, Db.PHIEUTHU phieuthucanin)
        {
            InitializeComponent();
            this.btnHuyPT.Enabled = false;
            this.btnInPT.Enabled = true;
            this.btnLuuPT.Enabled = false;
            this.btnThoat.Enabled = true;
            this.btnVietPTmoi.Enabled = false;
            this.groupBox1.Enabled = false;
            this.frmXemphieuthu = frm;
            this.PhieuThuCanin = phieuthucanin;
        }


        private void btnHuyPT_Click(object sender, EventArgs e)
        {
            this.txtSoCT.ResetText();
            this.txtSoPT.ResetText();
            this.numericUDSotienPT.ResetText();
            this.txtSoPT.Focus();
            this.dateTimePicker1.ResetText();
            this.txtMaNV.ResetText();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnVietPTmoi_Click(object sender, EventArgs e)
        {
            this.txtSoCT.ResetText();
            this.txtSoPT.ResetText();
            this.numericUDSotienPT.ResetText();
            this.txtSoPT.Focus();

            this.btnHuyPT.Enabled = true;
            this.btnInPT.Enabled = false;
            this.btnLuuPT.Enabled = true;
            this.btnThoat.Enabled = true;
            this.btnVietPTmoi.Enabled = false;
            this.groupBox1.Enabled = true;
            this.txtMaNV.Text = global.MaNhanVienDangNhap;
        }

        private void btnLuuPT_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtSoCT.Text.Trim()))
            {
                MessageBox.Show("Số chứng từ không được rỗng", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (string.IsNullOrEmpty(this.txtSoPT.Text.Trim()))
            {
                MessageBox.Show("Số phiếu thu không được rỗng", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (this.numericUDSotienPT.Value==0)
            {
                MessageBox.Show("Số tiền thu chưa nhập", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            var sopt = (from m in db.PHIEUTHUs
                        where m.SoPhieuThu == this.txtSoPT.Text.Trim()
                        select m).ToList();
            if (sopt.Count > 0)
            {
                MessageBox.Show("Số phiếu thu bị trùng!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            var soct = (from m in db.CHUNGTUs
                        where m.SoChungTu == this.txtSoCT.Text.Trim()
                        select m).ToList();
            if (soct.Count == 0)
            {
                MessageBox.Show("Không tồn tại chứng từ!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            var phieuthu = new NIEN_LUAN.Db.PHIEUTHU
            {                
                SoPhieuThu=this.txtSoPT.Text,
                SoChungTu=this.txtSoCT.Text,
                NgayLap = DateTime.Now,
                MaNhanVien =global.MaNhanVienDangNhap,
                SoTien=this.numericUDSotienPT.Value,                
            };


            //var no = (from m in db.CHUNGTUs
            //          where (m.MaKhachHang == this.txtKH.Text)
            //          select new
            //          {
            //              MaKhachHang = m.KHACHHANG.MaKhachHang,
            //              TenKhachHang = m.KHACHHANG.TenKhachHang,

            //              SoTienNo = m.TongTien - ((from k in db.PHIEUTHUs
            //                                        where k.SoChungTu == m.SoChungTu
            //                                        select k).Sum(s => s.SoTien))

            //                                        - m.TienDaTra//.Sum(s => s.SoTien) + m.TienDaTra

            //          }).ToList();
            //this.bsNo.DataSource = no;
            //this.bsNo.ResetBindings(true);
            //var no2 = (from m in no
            //           from x in no
            //           where m.MaKhachHang == x.MaKhachHang
            //           select new
            //           {
            //               MaKhachHang = m.MaKhachHang,
            //               TenKhachHang = m.TenKhachHang,
            //               SoTienNo = no.Sum(k => k.SoTienNo)////????


            //           }).Distinct().ToList();
            //var no3 = (from m in no2
            //           where ((m.SoTienNo == 0) || (no2.c))
            //           select m).ToList();


            //if (no3.Count <= 0)
            //{

            //    MessageBox.Show("da het no", "thong bao", MessageBoxButtons.OK);
            //    return;
            //}






            db.PHIEUTHUs.InsertOnSubmit(phieuthu);
            db.SubmitChanges();
            this.DialogResult = DialogResult.OK;
            MessageBox.Show("Tạo phiếu thu thành công!","Thông báo",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
            this.btnHuyPT.Enabled = false;
            this.btnInPT.Enabled = true;
            this.btnLuuPT.Enabled = false;
            this.btnThoat.Enabled = true;
            this.btnVietPTmoi.Enabled = true;
            this.groupBox1.Enabled = false;
            this.Close();
        }

        private void btnChon_Click(object sender, EventArgs e)
        {
            Form frmChonCT = new FormTimKiemChungTu(this);
            frmChonCT.ShowDialog();
        }

        private void FormLapPhieuThu_Load(object sender, EventArgs e)
        {
            if ((frmXemPhieuThu != null && PhieuThuCanin != null) || (frmXemphieuthu != null && PhieuThuCanin != null))
            {
                this.txtSoPT.Text = PhieuThuCanin.SoPhieuThu;
                this.txtSoCT.Text=PhieuThuCanin.SoChungTu;
                this.txtMaNV.Text = PhieuThuCanin.MaNhanVien;
                this.txtKH.Text=PhieuThuCanin.CHUNGTU.MaKhachHang;
                this.numericUDSotienPT.Value = PhieuThuCanin.SoTien ?? 0;                               
                this.dateTimePicker1.Value = PhieuThuCanin.NgayLap ?? DateTime.Now;

                //var ctphieuthu = (from m in db.PHIEUTHUs
                //                      where m.SoPhieuThu == PhieuThuCanin.SoPhieuThu
                //                      select m).ToList;
                //                      //select new Db.ChitietNhapXuat
                //                      //{
                //                      //    //DonGia = m.DonGiaXuat ?? 0,
                //                      //    //GiaNhap=m.DonGiaNhap??0,
                //                      //    MaHang = m.MaHang,
                //                      //    SoLuong = m.SoLuongNhap ?? 0,
                //                      //    TenHang = m.MATHANG.TenHang

                //                      //}).ToList();

                //this.bsphieuthu.DataSource = ctphieuthu;
            }
        }

        private void btnInPT_Click(object sender, EventArgs e)
        {
            Form frmInCTPT = new FormReportChiTietPhieuThu(this);
            frmInCTPT.ShowDialog();
        }
    }
}
